package com.eric.enderslastgame.threads;

import android.util.Log;
import android.view.SurfaceHolder;

import com.eric.enderslastgame.EndersGamePanel;

public class LevelThread extends Thread {

	private static final String TAG = LevelThread.class.getSimpleName();
	private final int SLEEP_TIME_LEVEL = 10000;

	// Surface holder that can access the physical surface
	private SurfaceHolder surfaceHolder;
	// The actual view that handles inputs
	// and draws to the surface
	private EndersGamePanel gamePanel;

	public LevelThread(SurfaceHolder surfaceHolder, EndersGamePanel gamePanel) {
		super();
		this.surfaceHolder = surfaceHolder;
		this.gamePanel = gamePanel;
	}

	@Override
	public void run() {
		while (EndersGamePanel.inGame) {
			if (gamePanel.level < 20) {
				gamePanel.level += 1;
				int delay = (int) (3 * (gamePanel.level ^ 2) + 600);
				
				gamePanel.bugGenSleepTime = gamePanel
						.genBugGenSleep(gamePanel.level); // Decrease the time between Bugger spawns
				gamePanel.setSwitchSleepTime = gamePanel.genSetSwitchSleep(gamePanel.level);
				
				Log.d(TAG, "Level Thread sleep delay set to: " + delay);
				Log.d(TAG, "Level increased to " + gamePanel.level);
				try {
					sleep(SLEEP_TIME_LEVEL);
				} catch (InterruptedException e) {
					System.out.println("Couldn't properly sleep the Level Thread");
				}
			}
		}
	}
}
